﻿
using Xacml.Core.Interfaces;
using System;
using System.Collections;
using System.Security.Permissions;
using System.Xml;

namespace Xacml.Core.Extensions
{
  public class DefaultRuleCombiningAlgorithmRepository : IRuleCombiningAlgorithmRepository
  {
    private Hashtable _algorithms = new Hashtable();

    [PermissionSet(SecurityAction.Demand, XML = "<PermissionSet class=\"System.Security.PermissionSet\"\r\n               version=\"1\">\r\n   <IPermission class=\"System.Security.Permissions.ReflectionPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\n                version=\"1\"\r\n                Flags=\"TypeInformation\"/>\r\n</PermissionSet>\r\n")]
    public void Init(XmlNode configNode)
    {
      foreach (XmlNode xmlNode in configNode.SelectNodes("./ruleCombiningAlgorithm"))
      {
        IRuleCombiningAlgorithm combiningAlgorithm = (IRuleCombiningAlgorithm) Activator.CreateInstance(Type.GetType(xmlNode.Attributes["type"].Value));
        this._algorithms.Add((object) xmlNode.Attributes["id"].Value, (object) combiningAlgorithm);
      }
    }

    public IRuleCombiningAlgorithm GetRuleCombiningAlgorithm(string ruleCombiningAlgorithmId)
    {
      return this._algorithms[(object) ruleCombiningAlgorithmId] as IRuleCombiningAlgorithm;
    }
  }
}
